<template>
  <el-dialog
    title="新增企业"
    :visible.sync="dialogFormVisible"
    width="600px"
    class="add_company"
    center
    @closed="closeHandler"
  >
    <el-form :model="form" ref="addForm" :rules="rules">
      <el-form-item
        label="企业编号"
        prop="rid"
        :label-width="formLabelWidth"
        required
      >
        <el-input v-model="form.rid" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item
        label="企业名称"
        prop="name"
        :label-width="formLabelWidth"
        required
      >
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item
        label="企业简称"
        prop="short_name"
        :label-width="formLabelWidth"
      >
        <el-input v-model="form.short_name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="企业简介" prop="intro" :label-width="formLabelWidth">
        <el-input v-model="form.intro" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item
        label="企业备注"
        prop="remark"
        :label-width="formLabelWidth"
      >
        <el-input v-model="form.remark" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="resetForm">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addSubject } from '../../../api/subject.js'
export default {
  name: 'add',
  data () {
    return {
      dialogFormVisible: false,
      form: {
        rid: '',
        name: '',
        short_name: '',
        intro: '',
        remark: ''
      },
      formLabelWidth: '120px',
      // 校验规则
      rules: {
        rid: [{ required: true, message: '学科编号不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '学科名称不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 关闭表单
    closeHandler () {
      // 清空表单
      this.$refs.addForm.resetFields()
    },
    submitForm () {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          // 调用接口新增学科
          addSubject(this.form)
            .then(res => {
              // 提示用户
              this.$message.success('新增成功')
              // 关闭对话框
              this.dialogFormVisible = false
              // 通知父组件更新页面数据
              this.$emit('added')
            })
            .catch(err => {
              console.log(err)
            })
        } else {
          this.$message.error('格式有误!')
          return false
        }
      })
    },
    resetForm () {
      this.dialogFormVisible = false
    }
  }
}
</script>

<style lang="less">
.add_company {
  // 头部样式
  .el-dialog__header {
    background: linear-gradient(to right, #01c6fa 28%, #1493fa 96%);
  }
  // 头部字体图标样式
  .el-dialog__headerbtn .el-dialog__close {
    color: white;
  }
}
</style>
